// pages/registered/registered.js
import api from '../../Sendingmethod/Sendingmethod'
import encrypt from "../../utils/crypto-util.js"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        //是否输入完整
        inpublen: false,
        //账号
        account: '',
        //密码
        password: '',
        //验证码
        // captcha: '',
        checked: '',
        nihei: false,
        resobj: {

        }
    },
    /**
     * 输入事件
     */
    inputfn(e) {
     
        if(e?.target){
            console.log(e?.target?.dataset?.index, e.detail.value, '输入事件');
            if (e?.target.dataset.index == '1') {
                console.log('触发账号');
                this.setData({
                    account: e.detail.value
                })
            }
            if (e?.target.dataset.index == '2') {
                this.setData({
                    password: e.detail.value
                })
            }
            if (e?.target.dataset.index == '3') {
                this.setData({
                    captcha: e.detail.value
                })
            }
        }
        if (this.data.account !== '' && this.data.password !== '') {
            console.log('触发验证');
            this.setData({
                inpublen: true
            })
        } else {
            this.setData({
                inpublen: false
            })
        }
    },
    onChange(event) {
        console.log("我点击了chekd", event.detail);
        this.setData({
            checked: event.detail,
            nihei: false
        });
    },
    loginfn() {
        console.log('下一步');
        wx.navigateTo({
            url: '/pages/personal/personal',
        })
        // if (!this.data.checked) {
        //     this.setData({
        //         nihei: true
        //     })
        // } else {

        // }
    },
    /**
     * 
     * 字符串截取
     */
    getQueryParam(url, key) {
        const paramsString = url.split('?')[1];
        if (!paramsString) return null;
        return paramsString.split('&')
            .find(param => param.startsWith(`${key}=`))
            ?.split('=')[1];
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if (options.q) {
            //微信扫码带参 进入小程序
            let newUrl = decodeURIComponent(options.q)
            console.log(newUrl, '我是微信进入参数');
            let newsn = this.getQueryParam(newUrl, 'sn')
            console.log(newsn, '截取出的字符串');
            let newstr = JSON.parse(encrypt.aesDecrypt(newsn))
            console.log(newstr, '解密后的对象');
            this.setData({
                resobj: newstr
            })
        }
    },
    /**
     * 推广注册
     */
    // async getpromoteregister() {
    //     wx.showLoading({
    //         mask: true
    //     })
    //     let res = await promoteregister()
    //     if (res.code == 0) {


    //     } else {

    //     }
    // },
    async getpromoteregister(data) {
        console.log(data, '传输数据');
        // wx.showLoading({
        //     title: '登陆中',
        //     mask: true
        // })
        console.log({
            username: this.data.account,
            password: this.data.password,
            ...this.data.resobj
        }, '注册账号数据');
        try {
            let res = await api.promoteregister({
                username: this.data.account,
                password: this.data.password,
                ...this.data.resobj
            })

            console.log('登录返回信息', res);
            console.log(res);
            if (res.code == 0) {
                console.log('登录成功', res.data);
                let userdata = JSON.stringify({
                    username: this.data.account,
                    password: this.data.password
                })
                // 存储信息
                wx.setStorageSync('userdata', userdata)
                wx.setStorageSync("token", res.data.token)
                wx.setStorageSync("userid", res.data.uid)
                let userinfo = JSON.stringify(res.data.userinfo)
                wx.setStorageSync("userinfo", userinfo)
                console.log(wx.getStorageSync('userinfo'), '当前存储的');
                wx.hideLoading()

                if (res.data.userinfo.status == 0) {
                    wx.showToast({
                        title: '注册成功',
                        icon: 'success',
                        duration: 2000
                    })
                    wx.navigateTo({
                        url: '/pages/personal/personal',
                    })
                }
                if (res.data.userinfo.status == 1) {
                    wx.showToast({
                        title: '该账号在审核中',
                        icon: 'loading',
                        duration: 2000
                    })
                }
                if (res.data.userinfo.status == 3) {
                    wx.showToast({
                        title: '该账号已停用',
                        icon: 'loading',
                        duration: 2000
                    })
                }
                if (res.data.userinfo.status == 2) {
                    wx.showToast({
                        title: '登录成功',
                        icon: 'success',
                        duration: 2000
                    })
                    wx.switchTab({
                        url: '/pages/orders/orders',
                    })
                }
                if (res.data.userinfo.status == -1) {
                    console.log('11111');
                    wx.showToast({
                        title: '审核未通过',
                        icon: 'loading',
                        duration: 2000
                    })
                    wx.navigateTo({
                        url: '/pages/personal/personal',
                    })
                }
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error',
                    duration: 2000
                })
            }
        } catch (error) {
            wx.showToast({
                title: '请求失败',
                icon: 'error',
                duration: 2000
            })
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        if (wx.getStorageSync('userdata')) {
            let obj = wx.getStorageSync('userdata')
            let data = JSON.parse(obj)
            this.setData({
                account: data.username,
                password: data.password
            })
            this.inputfn()
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})